import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  /* {
    path: '/documentation',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/documentation/index'),
        name: 'Documentation',
        meta: { title: 'Documentation', icon: 'documentation', affix: true }
      }
    ]
  },
  {
    path: '/guide',
    component: Layout,
    redirect: '/guide/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/guide/index'),
        name: 'Guide',
        meta: { title: 'Guide', icon: 'guide', noCache: true }
      }
    ]
  }, */
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/blacklist/list'
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/blackdatasource',
    component: Layout,
    // hidden: true,
    children: [
      {
        path: 'list',
        component: () => import('@/views/blackdatasource/list'),
        name: 'BlackDataSource',
        meta: { title: '黑名单数据源', icon: 'yuantougonghuo', roles: ['admin'], affix: true }
      }
    ]
  },
  {
    path: '/blacklist',
    component: Layout,
    // hidden: true,
    children: [
      {
        path: 'list',
        component: () => import('@/views/blacklist/list'),
        name: 'BlackList',
        meta: { title: '黑名单', icon: 'heimingdan', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/accesslimit',
    component: Layout,
    // hidden: true,
    children: [
      {
        path: 'list',
        component: () => import('@/views/accesslimit/list'),
        name: 'AccessLimit',
        meta: { title: '访问频率控制', icon: 'xianzhiremen', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/changelimit',
    component: Layout,
    // hidden: true,
    children: [
      {
        path: 'list',
        component: () => import('@/views/changelimit/list'),
        name: 'ChangeLimit',
        meta: { title: '变化频率控制', icon: 'fangwenxianzhi', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/expression',
    component: Layout,
    // hidden: true,
    children: [
      {
        path: 'list',
        component: () => import('@/views/expression/list'),
        name: 'Expression',
        meta: { title: '表达式', icon: 'biaodashijisuan', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/collection',
    component: Layout,
    // hidden: true,
    children: [
      {
        path: 'list',
        component: () => import('@/views/collection/list'),
        name: 'Collection',
        meta: { title: '策略集', icon: 'celve', roles: ['admin'] }
      }
    ]
  }
]
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
